<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          v-hasPermi="['purchase:productStatistical:query']"
          @click="handleSearch"
        >
          搜索
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport(1)"
          v-hasPermi="['purchase:productStatistical:export']"
        >
          明细数据导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport(2)"
          v-hasPermi="['purchase:productStatistical:export']"
        >
          物料统计导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport(3)"
          v-hasPermi="['purchase:productStatistical:export']"
        >
          厂商统计导出
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="820px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="12">
          <el-form-item label="审核日期">
            <el-date-picker
              v-model="dateRange"
              size="small"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              style="width: 215px"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入库仓库" prop="rkck">
            <el-select v-model="queryParams.rkck" placeholder="请选择仓库">
              <el-option
                v-for="item in allStorageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="采购单号" prop="pomDjbh">
            <el-input
              v-model="queryParams.pomDjbh"
              placeholder="请输入采购单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售单号" prop="somDjbh">
            <el-input
              v-model="queryParams.somDjbh"
              placeholder="请输入销售单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料代码" prop="itmWldm">
            <el-input
              v-model="queryParams.itmWldm"
              placeholder="请输入物料代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户编码" prop="itmCustwldm">
            <el-input
              v-model="queryParams.itmCustwldm"
              placeholder="请输入客户编码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="厂商代码">
            <el-select v-model="queryParams.csdm" clearable size="small">
              <el-option
                v-for="dict in csmOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
            -
            <el-select v-model="queryParams.csdmEnd" clearable size="small">
              <el-option
                v-for="dict in csmOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <!-- 子表TAB-->
    <el-tabs v-model="activeTab">
      <el-tab-pane label="明细数据" name="first">
        <el-table
          highlight-current-row
          v-loading="loading"
          :data="detailList"
          border
          stripe
          :height="tableH"
          @scrollBottom="load"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" width="40" align="center" />
          <el-table-column
            label="采购单号"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="productCgdh"
          />
          <el-table-column
            label="入库单号"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="productRkdh"
          />
          <el-table-column
            label="销售单号"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="productSodh"
          />
          <el-table-column
            label="厂商名称"
            :show-overflow-tooltip="true"
            width="150px"
            prop="productCsmc"
          />
          <el-table-column
            label="厂商编码"
            :show-overflow-tooltip="true"
            width="135px"
            prop="productCsdm"
          />
          <el-table-column
            label="入库日期"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="productRkdhTime"
          />
          <el-table-column
            label="客户编号"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="itmCustwldm"
          />
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            width="135px"
            align="center"
            prop="productWldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            width="350px"
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="80px"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="itmDwOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="最小包装"
            :show-overflow-tooltip="true"
            width="80px"
            align="center"
            prop="itmMinbz"
          />
          <el-table-column
            label="仓库"
            :show-overflow-tooltip="true"
            width="80"
            align="center"
            prop="pomRkck"
          >
            <template #default="scope">
              <dict-tag
                :options="allStorageOptions"
                :value="scope.row.pomRkck"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="采购数量"
            :show-overflow-tooltip="true"
            width="80px"
            align="center"
            prop="pomCgsl"
          />
          <el-table-column
            label="入库数量"
            :show-overflow-tooltip="true"
            width="80px"
            align="center"
            prop="rwdRksl"
          />
          <el-table-column
            label="备注"
            :show-overflow-tooltip="true"
            width="150px"
            prop="pomDesc"
          />
          <el-table-column
            label="开单人员"
            :show-overflow-tooltip="true"
            width="80px"
            align="center"
            prop="pomJlry"
          />
          <el-table-column
            label="开单日期"
            :show-overflow-tooltip="true"
            width="150px"
            align="center"
            prop="pomJlrq"
          />
          <el-table-column
            label="审核人员"
            :show-overflow-tooltip="true"
            width="80px"
            align="center"
            prop="pomShry"
          />
          <el-table-column
            label="审核日期"
            :show-overflow-tooltip="true"
            width="150px"
            align="center"
            prop="pomShrq"
          />
          <el-table-column
            label="计划日期"
            :show-overflow-tooltip="true"
            width="150px"
            align="center"
            prop="pomJhrq"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="按物料统计" name="second">
        <el-table
          highlight-current-row
          v-loading="loading"
          :data="wldmList"
          border
          stripe
          :height="tableH"
          @scrollBottom="load2"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" width="40" align="center" />
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            width="135"
            align="center"
            prop="rwdWldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            width="400"
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="80"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="itmDwOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>

          <el-table-column
            label="采购数量"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="podCgsl"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="按厂商统计" name="third">
        <el-table
          highlight-current-row
          v-loading="loading"
          :data="csdmList"
          border
          stripe
          :height="tableH"
          @scrollBottom="load3"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" type="index" width="40" align="center" />
          <el-table-column
            label="厂商代码"
            :show-overflow-tooltip="true"
            width="130"
            align="center"
            prop="rwmCsdm"
          />
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            width="130"
            align="center"
            prop="rwdWldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            width="400"
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="80"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="itmDwOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="采购数量"
            width="100"
            :show-overflow-tooltip="true"
            align="center"
            prop="podCgsl"
          />
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { listProStatis, exportProStatis } from "@/api/purchase/productSelect";
import { getAllCgList, getAllCgListNormal } from "@/api/project/mstr";
import { getAllCsm } from "@/api/purchase/csm";
import { getAll } from "@/api/repertory/Storage";

export default {
  name: "ProductStatistical",
  data() {
    return {
      activeTab: "first",
      // 遮罩层
      loading: true,
      tableH: document.body.clientHeight - 240,
      // 导出遮罩层
      exportLoading: false,
      dateRange: [
        new Date(new Date().getFullYear(), new Date().getMonth() - 1),
        this.$moment().endOf("month").toDate(),
      ],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      itmDwOptions: [],
      csmOptions: [],
      allStorageOptions: [],
      //采购明细总数 和 详细表
      detailTotal: 0,
      detailList: [],
      //物料统计 总数 和 List
      wldmTotal: 0,
      wldmList: [],
      //厂商统计 总数 和 List
      csdmTotal: 0,
      csdmList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        pomShrq: null,
        pomShrqEnd: null,
        pomDjbh: null,
        somDjbh: null,
        itmWldm: null,
        itmCustwldm: null,
        csdm: null,
        csdmEnd: "HD999",
        excelIndex: 1,
        rkck: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_unit").then((response) => {
      this.itmDwOptions = response.data;
    });
    getAll().then((response) => {
      this.allStorageOptions = response.data;

      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    getAllCsm().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.csmCsdm + " " + i.csmCsmc;
        i.dictValue = i.csmCsdm + "";
        i.listClass = "default";
      }
      this.csmOptions = response.data;
      this.queryParams.csdm = this.csmOptions[0].csmCsdm;
    });
  },
  methods: {
    /** 分页跳转 */
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 查询待生产采购明细列表 */
    getList() {
      this.loading = true;
      this.queryParams.pomShrq = null;
      this.queryParams.pomShrqEnd = null;
      if (this.dateRange !== null) {
        this.queryParams.pomShrq = this.moment(this.dateRange[0]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
        this.queryParams.pomShrqEnd = this.moment(this.dateRange[1]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      listProStatis(this.queryParams).then((response) => {
        // 明细数据
        this.detailList = this.detailList.concat(response.data.detail.rows);
        // this.detailList = response.data.detail.rows;
        this.detailTotal = response.data.detail.total;
        // 按物料统计
        this.wldmList = this.wldmList.concat(response.data.wldm.rows);
        this.wldmTotal = response.data.wldm.total;
        // 按厂商统计
        this.csdmList = this.csdmList.concat(response.data.csdm.rows);
        this.csdmTotal = response.data.csdm.total;
        this.loading = false;
      });
    },
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.detailTotal / this.queryParams.pageSize)
      ) {
        // this.$message.success("加载下一页");
        this.queryParams.pageSize = 20;
        this.queryParams.pageNum++;
        this.getList();
      }

      return 0;
    },
    load2() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.wldmTotal / this.queryParams.pageSize)
      ) {
        // this.$message.success("加载下一页");
        this.queryParams.pageSize = 20;
        this.queryParams.pageNum++;
        this.getList();
      }
      // if(this.queryParams.pageNum < Math.ceil(this.csdmTotal / this.queryParams.pageSize)){
      //   // this.$message.success("加载下一页");
      //   this.queryParams.pageSize = 20
      //   this.queryParams.pageNum++
      //   this.getList()
      // }
      return 0;
    },
    load3() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.csdmTotal / this.queryParams.pageSize)
      ) {
        // this.$message.success("加载下一页");
        this.queryParams.pageSize = 20;
        this.queryParams.pageNum++;
        this.getList();
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.openSearch = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.wldmList = [];
      this.detailList = [];
      this.csdmList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeProductQgdhTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSearch() {
      this.title = "查询生产采购统计分析";
      this.openSearch = true;
    },
    /** 导出按钮操作 */
    handleExport(exportIndex) {
      this.queryParams.excelIndex = exportIndex;
      const queryParams = this.queryParams;
      exportProStatis(queryParams).then((res) => {
        this.download(res.msg);
      });
    },
  },
};
</script>
